//*************************************************
//Program #4       LRU.cpp
//*************************************************
//Names: Stephen Durfrey,  Andrew Brock
//Due: April 23, 2010
//Operating Systems
//Related Files:  LRU.h
// Least-Recently-Used PGA: Associates with each page the time
//  of that pages' last use.

#include "LRU.h"
#include <string>
#include <stdlib.h>
#include <list>

using namespace std;
//Default constructor
LRU::LRU()
{
}
//*************************************************
//LRU::init - initializes the varibles based on the variables
// passed into the program.
//*************************************************
void LRU::init(int frames,  list<int> ref_str)
{
	num_frames = frames;
	ref_string_list = ref_str;
	num_faults = 0;
	ref_page = ref_string_list.begin();
}
//*************************************************
//LRU::checkFrame - checks to see if the value "check"
// is in the frames.  If it is its location is passed back
// otherwise check == frames.end()
//*************************************************
list<int>::iterator LRU::checkFrame(list<int>::iterator check)
{
	bool found = false;
	list<int>::iterator value, begin, end;

	value = end = framesList.end();

	for(begin = framesList.begin(); begin != end && !found; begin++)
	{
		if(*begin == *check)
		{
			value = begin;
			found = true;
		}
	}

	return value;

}
//*************************************************
//LRU::checkFrame - checks to see if the value "s_val" is inside
// of framesList.  If it isn't inFrame is false.
//*************************************************
list<int>::iterator LRU::searchFrames(list<int>::iterator s_val, bool &inFrame)
{
	list<int>::iterator begin, end, f_val;

	end = framesList.end();

	f_val = s_val;

	for(begin = framesList.begin(); begin != end && !inFrame; begin++)
	{
		

		if(*begin == *f_val)
		{		
			inFrame = true;
			f_val = begin;
		}
	}

	return f_val;
}
//*************************************************
//LRU::findPage - this is where the loop is that the LRU
// page replacement algorithm is tested.
//*************************************************
void LRU::findPage()
{
	list<int>::iterator value, position, begin, end, checker;
	bool inFrame = false;
	int temp;

	end = ref_string_list.end();
	begin = ref_string_list.begin();
	
	
	for(begin = ref_string_list.begin(); begin != end; begin++, ref_page++)
	{//BEGIN FOR
		if(framesList.size() != num_frames)
		{//BEGIN IF
			//first checks to see if *ref_page is already in frames
			checker = checkFrame(begin);
			if(checker == framesList.end())
			{//Frames has an empty slot
				framesList.push_front(*begin);
				num_faults++;
			}
		}//END IF
		else
		{//BEGIN ELSE
			//Checks to see if value is in the frames.
			value = checkFrame(begin);
			//If it isn't remove bottom value and add it to the top
			if(value == framesList.end())
			{
				framesList.pop_back();
				framesList.push_front(*begin);
				num_faults++;
			}
			//if it is in frames move it to the top of list
			else			
			{
				temp = *value;
				framesList.erase(value);
				framesList.push_front(temp);
			}
		}//END ELSE

		inFrame = false;
	}
}
//*************************************************
//LRU::run - executes the algorithm and then outputs
// the results of values num_frames and num_faults
//*************************************************
void LRU::run()
{
	char value[50];
	string value_param;

	findPage();

	sprintf(value, "%d , %d", num_frames, num_faults);

	value_param = (string)value;
	addCsvString("LRU.txt", value_param );
}

